package com.rattsoftware.server.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import com.rattsoftware.server.admin.model.Department;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.dao.DepartmentDAO;

/**
 * Provides a hibernate implementation
 * of the <code>DepartmentDAO</code> data access <code> interface.</code>
 */
@Repository("departmentDAO")
public class HibernateDepartmentDAO extends BaseDAOImpl<Department> implements DepartmentDAO {

	@Override
	public void deleteDepartment(Department department) {
		remove(department);
	}

	@Override
	public Department getDepartment(Integer id) {
		return super.find(id);
	}

	@Override
	public PagingLoadResult<Department> getDepartmentList(
			PagingLoadConfig pagingLoadConfig) {
		Search departmentSearch = getSearchFromLoadConfig(pagingLoadConfig, "id");
		SearchResult<Department> result = searchAndCount(departmentSearch);
		return getPagingLoadResult(pagingLoadConfig, result);
	}

	@Override
	public PagingLoadResult<Department> getDepartmentList(Integer facultyId,
			PagingLoadConfig pagingLoadConfig) {
		Search departmentSearch = getSearchFromLoadConfig(pagingLoadConfig, "id");
		departmentSearch.addFilterEqual("faculty_id", facultyId);
		SearchResult<Department> result = searchAndCount(departmentSearch);
		return getPagingLoadResult(pagingLoadConfig, result);
	}

	@Override
	public List<Department> getDepartments() {
		Search s = new Search();
		s.addSort("name", false);
		return search(s);
	}

	@Override
	public void saveDepartment(Department department) {
		save(department);
	}

	@Override
	public Department getDepartment(String departmentName) {
		return super.searchUniqueByPropertyEqual("name", departmentName);
	}
}